package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.util.DataCleanManager;
import com.hanhanxiaochong.zhongxiangchuanmei.util.MessageEvent;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.main.act.LoginActivity;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/17.
 * Desc   : 系统设置act
 */

public class SettingActivity extends BaseActivity {

    @BindView(R.id.img_left)
    ImageView imgLeft;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_trush)
    TextView txtTrush;
    @BindView(R.id.txt_version_code)
    TextView txtVersionCode;
    @BindView(R.id.ll_update_userinfo)
    LinearLayout llUpdateUserinfo;
    @BindView(R.id.ll_bind_phone)
    LinearLayout llBindPhone;
    @BindView(R.id.ll_userinfo_check)
    LinearLayout llUserinfoCheck;
    @BindView(R.id.ll_update_loginpwd)
    LinearLayout llUpdateLoginpwd;
    @BindView(R.id.ll_update_paypwd)
    LinearLayout llUpdatePaypwd;
    @BindView(R.id.ll_clear)
    LinearLayout llClear;
    @BindView(R.id.ll_check_update)
    LinearLayout llCheckUpdate;
    @BindView(R.id.btn_exit_login)
    Button btnExitLogin;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_setting);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void init() {
        setTopView();
        File file = new File(this.getCacheDir().getPath());
        try {
            txtTrush.setText(DataCleanManager.getCacheSize(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setTopView() {
        txtTitle.setText("设置");
        imgLeft.setImageResource(R.mipmap.icon_left);
    }

    @OnClick({R.id.ll_leftview, R.id.ll_update_userinfo, R.id.ll_bind_phone, R.id
            .ll_userinfo_check, R.id.ll_update_loginpwd, R.id.ll_update_paypwd, R.id.ll_clear, R
            .id.ll_check_update, R.id.btn_exit_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.ll_update_userinfo:
                Intent intent = new Intent(this, UpdateUserInfoActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_bind_phone:
                Intent bindIntent = new Intent(this, BindPhoneActivity.class);
                startActivity(bindIntent);
                break;
            case R.id.ll_userinfo_check:
                Intent idIntent = new Intent(this, IDCardRenzhengActivity.class);
                startActivity(idIntent);
                break;
            case R.id.ll_update_loginpwd:
                startActivity(ChangePasswordActivity.createIntent(this, "1"));
                break;
            case R.id.ll_update_paypwd:
                startActivity(ChangePasswordActivity.createIntent(this, "2"));
                break;
            case R.id.ll_clear:
                clearCacheDialog();
                break;
            case R.id.ll_check_update:
                ToastUtils.showToast("已是最新版本");
                break;
            case R.id.btn_exit_login:
                SharedPreferencesUtil.getsInstances(this).clearLoginInfo();
                startActivity(LoginActivity.createIntent(this));
                EventBus.getDefault().post(new MessageEvent("login_out"));
                finish();
                break;
        }
    }

    private void clearCacheDialog() {
        new AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("确定清除缓存？")
                .setPositiveButton("确定", (dialog, which) -> {
                    DataCleanManager.cleanInternalCache(getApplicationContext());
                    txtTrush.setText(String.format(getString(R.string.cache_size), 0.00));
                })
                .setNegativeButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                })
                .show();
    }

}
